<template>
	<el-form ref="formRef" size="large" class="register-content-form" :model="state.ruleForm" :rules="rules">
		<el-form-item class="register-animation1" prop="username">
			<el-input type="text" :placeholder="$t('message.accountPassword.accountPlaceholder1')" v-model="ruleForm.username"
				 clearable autocomplete="off" @input="updateSharedState">
				<template #prefix>
					<el-icon class="el-input__icon"><ele-User /></el-icon>
				</template>
			</el-input>
		</el-form-item>
		<el-form-item class="register-animation2" prop="password">
			<el-input :type="isShowPassword ? 'text' : 'password'" :placeholder="$t('message.accountPassword.accountPlaceholder2')"
				v-model="ruleForm.password" clearable autocomplete="off" @input="updateSharedState">
				<template #prefix>
					<el-icon class="el-input__icon"><ele-Unlock /></el-icon>
				</template>
				<template #suffix>
					<i class="iconfont el-input__icon register-content-password"
						:class="isShowPassword ? 'icon-yincangmima' : 'icon-xianshimima'"
						@click="isShowPassword = !isShowPassword">
					</i>
				</template>
			</el-input>
		</el-form-item>
		<el-form-item class="register-animation3"  prop="password_concerned">
				<el-input :type="isShowPassword ? 'text' : 'password'" :placeholder="$t('message.accountPassword.accountPlaceholder3')"
					v-model="ruleForm.password_concerned" clearable autocomplete="off" @input="updateSharedState">
					<template #prefix>
						<el-icon class="el-input__icon"><ele-Unlock /></el-icon>
					</template>
          <template #suffix>
            <i class="iconfont el-input__icon register-content-password"
              :class="isShowPassword ? 'icon-yincangmima' : 'icon-xianshimima'"
              @click="isShowPassword = !isShowPassword">
            </i>
				  </template>
				</el-input>
		</el-form-item>
		<el-form-item class="register-animation4">
			<el-button type="primary" class="register-content-submit" round @keyup.enter="LoginClick" @click="LoginClick"
				:loading="loading.signIn">
				<span>{{ $t('message.accountPassword.accountBtnText1') }}</span>
			</el-button>
      <el-button class="register-content-submit" round @keyup.enter="NextClick" @click="NextClick">
				<span>{{ $t('message.accountPassword.accountBtnText2') }}</span>
			</el-button>
		</el-form-item>
	</el-form>
</template>

<script lang="ts">
import { toRefs, reactive, defineComponent, computed, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { FormRules } from 'element-plus';
import { useI18n } from 'vue-i18n';
import Cookies from 'js-cookie';
import { storeToRefs } from 'pinia';
import { useThemeConfig } from '/@/stores/themeConfig';
import { formatAxis } from '/@/utils/formatTime';
import { useUserInfo } from '/@/stores/userInfo';
import axios from "axios";
import {errorMessage} from "/@/utils/message";
export default defineComponent({
	name: 'registerAccount',
  props: {
    sharedState: {
      type: Object,
      default: () => ({}),
    },
  },
	setup(props, { emit }) {
		const { t } = useI18n();
		const storesThemeConfig = useThemeConfig();
		const { themeConfig } = storeToRefs(storesThemeConfig);
		const { userInfos } = storeToRefs(useUserInfo());
		const route = useRoute();
		const router = useRouter();
		const state = reactive({
			isShowPassword: false,
			ruleForm: {
				username: '',
				password: '',
        password_concerned: '',
			},
			loading: {
				signIn: false,
			},
		});
    const validatePass2 = (rule, value, callback) => {
			if (value === '') {
				callback(new Error('请再次输入密码'));
			} else if (value !== state.ruleForm.password) {
				callback(new Error('两次输入密码不一致!'));
			} else {
				callback();
			}
		};
    const updateSharedState = () => {
      emit('update-state', {
        username: state.ruleForm.username,
        password: state.ruleForm.password,
      });
      if(!state.ruleForm.username){
        emit('update-int',1)
      }
      else if(!state.ruleForm.password){
        emit('update-int',2)
      }
      else if(!state.ruleForm.password_concerned){
        emit('update-int',3)
      }
      else if(state.ruleForm.password_concerned!==state.ruleForm.password){
        emit('update-int',4)
      }
      else {
        emit('update-int',0)
      }
    };
		const rules = reactive<FormRules>({
			username: [
				{
          required: true,
          message: '请填写账号',
          trigger: 'blur'
        },

			],
			password: [
				{
					required: true,
					message: '请填写密码',
					trigger: 'blur',
				},
			],
      password_concerned: [
				{
					required: true,
					message: '请再次填写密码',
					trigger: 'blur',
				},
        {
					validator: validatePass2,
					trigger: 'blur',
				},
			],
		})
		// 时间获取
		const currentTime = computed(() => {
			return formatAxis(new Date());
		});
    const LoginClick = async () => {
      router.push('/login');
		};
    const NextClick = () => {
      if(!state.ruleForm.username){
        errorMessage("请输入账号")
      }
      else if(!state.ruleForm.password){
        errorMessage("请输入密码")
      }
      else if(!state.ruleForm.password_concerned){
        errorMessage("请再次输入密码")
      }
      else if(state.ruleForm.password_concerned!==state.ruleForm.password){
        errorMessage("密码不一致")
      }
      else {
        emit('switch-tab', 'idCard');
      }
		};
		return {
			LoginClick,
      NextClick,
      updateSharedState,
			state,
			rules,
			...toRefs(state),
		};
	},
});
</script>

<style scoped lang="scss">
.register-content-form {
	margin-top: 20px;

	@for $i from 1 through 4 {
		.register-animation#{$i} {
			opacity: 0;
			animation-name: error-num;
			animation-duration: 0.5s;
			animation-fill-mode: forwards;
			animation-delay: calc($i/10) + s;
		}
	}

	.register-content-password {
		display: inline-block;
		width: 20px;
		cursor: pointer;

		&:hover {
			color: #909399;
		}
	}
	.register-content-submit {
		width: 48%;
		letter-spacing: 2px;
		font-weight: 300;
		margin-top: 15px;
	}

  .Sign-content-submit {
		width: 48%;
		letter-spacing: 2px;
		margin-top: 15px;
	}
}
</style>
